package scalab.filters.scanners

/**
* A filter that generates a dependency for each folder contained in 
*	the specified <code>dirs</code> directories. This filters has a 
*	unique mandatory input <code>in</code>. If this input is 
*	provisionned by the <code>Universe</code>, a hard dependency is 
*	generated for each existing folder in the directories. Otherwise, 
*	a dependency is generated iff <code>in</code> received a 
*	dependency pointing on the same resource. By default, this filter  
* recurses in subdirectories. If you want to avoid this behavior, 
* it is possible to unset the recursive flag.
*
*	Note that this filter generates dependencies only for directories. 
* If you need dependencies for files, consider using 
*	scalab.lib.filters.ListDirs. In the case you need dependencies for both 
* files and directories, a good way to achieve this is to generate 
* a compound filter:
*	<p align="center">
* <code>val FilesAndDirs = ListDirs(myDir) union Dirs(myDir)</code>
*	</p>
*/
trait Dirs extends scalab.core.component.Scanner{
	
	import scalab.core.dependencies.{Dependency, HardDependency}

	override def scan(d: Dependency): Iterable[Dependency] = 
		if(d.isDirectory){
			val dirs = HardDependency((d.listFiles filter {f => f.isDirectory}))
			transmit(dirs)
			dirs
		}
		else
			Nil
}

object Dirs{

	def apply(recurse: Boolean): Dirs = new Dirs{
		override val recursive: Boolean = recurse
		override val name: String = "Dirs"
	}

	def apply: Dirs = apply(true)
}
